package com.sy.olddriver.service.impl;

import com.sy.olddriver.constant.AppConstant;
import com.sy.olddriver.dao.ProductDao;
import com.sy.olddriver.dao.impl.ProductDaoImpl;
import com.sy.olddriver.dto.Page;
import com.sy.olddriver.entity.Product;
import com.sy.olddriver.service.ProductService;
import com.sy.olddriver.vo.ProductQueryParam;

import java.util.List;

public class ProductServiceImpl implements ProductService {
    private ProductDao productDao = new ProductDaoImpl();

    @Override
    public List<Product> getNewestProducts() {
        return productDao.getNewestProducts();
    }

    @Override
    public List<Product> getHotestProducts() {
        return productDao.getHotestProducts();
    }

    @Override
    public Page<Product> getPagingProducts(ProductQueryParam param, Long pageNo) {
        Long total = productDao.getProductsCountByCondition(param);
        Page<Product> page = new Page<>();
        if (pageNo == null || pageNo <= 0) {
            pageNo = 1L;
        }
        page.setPageNo(pageNo);
        Long pageSize = AppConstant.GOODS_LIST_PAGE_SIZE;
        page.setPageSize(pageSize);
        page.setTotal(total);
        Long pageCount = (total + pageSize - 1) / pageSize;
        page.setPageCount(pageCount);
        List<Product> products = productDao.getProductsByCondition(param, (pageNo - 1) * pageSize, pageSize);
        page.setRows(products);
        return page;
    }

    @Override
    public Product getProductById(Integer pid) {
        return productDao.getProductById(pid);
    }
}
